package com.yugu.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 接口返回统一通过该类包装返回
 */
public class ResponseUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(ResponseUtil.class);

    public static <T> Result<T> addSuccess(T data) {
        Result<T> result = new Result<>();
        if (data instanceof Boolean) {
            Boolean ok = (Boolean) data;
            if (ok) {
                result.setHttpCode(HttpStatus.OK.value());
            } else {
                result.setHttpCode(HttpStatus.BAD_REQUEST.value());
            }

        } else {
            result.setHttpCode(HttpStatus.OK.value());
        }
        result.setMessage("操作成功");
        result.setData(data);
        return result;
    }


    public static <T> Result<T> addCustom(HttpStatus status, String message) {
        Result<T> result = new Result<>();
        result.setHttpCode(status.value());
        result.setMessage(message);
        result.setData(null);
        return result;
    }

    public static <T> Result<T> addCustom(int code, String message) {
        Result<T> result = new Result<>();
        result.setHttpCode(code);
        result.setMessage(message);
        result.setData(null);
        return result;
    }

    public static <T> Result<T> addError(String message) {
        Result<T> result = new Result<>();
        result.setHttpCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
        result.setMessage(message);
        result.setData(null);
        return result;
    }

    public static <T> Result<T> addWarn(String message) {
        Result<T> result = new Result<>();
        result.setHttpCode(Constants.WARN_CODE_VALUE);
        result.setMessage(message);
        result.setData(null);
        return result;
    }

    /**
     * 通过response对象响应信息
     *
     * @param response
     * @param content
     * @return void
     * @author sunwy
     * @date 2021/7/21
     */
    public static void response(HttpServletResponse response, Object content) {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        try (PrintWriter out = response.getWriter()) {
            String json = content.toString();
            out.append(json);
        } catch (IOException e) {
            LOGGER.error("response", e);
        }
    }

    public static <T> Result<T> addError(String message, String trace) {
        Result<T> result = new Result<>();
        result.setHttpCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
        result.setMessage(message);
        result.setData(null);
        return result;
    }

    /**
     * 数据结果包装
     *
     * @param <T>
     */
    public static class Result<T extends Object> {
        private int httpCode;
        private T data;
        private String message;
        public String getTraceId() {
            return traceId;
        }

        public Result<T> setTraceId(String traceId) {
            this.traceId = traceId;
            return this;
        }

        private String traceId;

        public int getHttpCode() {
            return httpCode;
        }

        public Result<T> setHttpCode(int code) {
            this.httpCode = code;
            return this;
        }

        public T getData() {
            return data;
        }

        public Result<T> setData(T data) {
            this.data = data;
            return this;
        }

        public String getMessage() {
            return message;
        }

        public Result<T> setMessage(String message) {
            this.message = message;
            return this;
        }

        public boolean isSuccess() {
            return HttpStatus.OK.value() == this.httpCode;
        }


    }

}
